# _*_ coding: utf-8 _*_
"""
@ 时间    ：2024/10/23 10:39
@ 作者    ：旺财
@ 文件    ：肿瘤预测模型.py
@ 说明    ：肿瘤预测模型
"""
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, roc_auc_score, roc_curve


# 1.读取数据
df = pd.read_excel('肿瘤数据.xlsx')
print(df.head(5))

# 2.提取特征与目标变量
x = df.drop(columns='肿瘤性质')
y = df['肿瘤性质']

# 3.划分训练集与测试集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=1)

# 4.搭建模型
mode = GaussianNB()
mode.fit(x_train, y_train)

# 5.模型预测
y_predict = mode.predict(x_test)
predict = pd.DataFrame()
predict['预测值'] = list(y_predict)
predict['实际值'] = list(y_test)
print(predict.head())

# 6.模型评估
# 6.1 准确度1
score1 = mode.score(x_test, y_test)
# print(score1)

# 6.2 准确度2
score2 = accuracy_score(y_test, y_predict)
print(f'准确度为:{round(score2*100, 2)}%')

# 6.3 AUC值
y_predict_proba = mode.predict_proba(x_test)
auc = roc_auc_score(y_test, y_predict_proba[:, 1])
print(f'AUC值为:{round(auc*100, 2)}%')

# 7.可视化 ROC曲线
fpr, tpr, _ = roc_curve(y_test, y_predict_proba[:, 1])
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.plot(fpr, tpr)
plt.title('ROC曲线')
plt.xlabel('fpr-误报率')
plt.ylabel('tpr-命中率')
plt.show()